/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.beetl.ext.fn.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;
import sample.web.dto.OrderDto;
import sample.web.model.Cate;
import sample.web.model.Order;
import sample.web.model.User;
import sample.web.service.CateService;
import sample.web.service.OrderService;
import sample.web.service.UserService;
import sample.web.util.LayerData;
import sample.web.util.RestResponse;

import javax.servlet.ServletRequest;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class PageController {

	@Autowired
	private UserService userService;
	@Resource
	private OrderService orderService;
	@Resource
	private CateService cateService;
	@RequestMapping("/")
	public String index(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", "Hello Spring Boot Beetl!");
//		List<User> list = userService.selectAll();
//		System.out.println(list);
		return "index";
	}
	
	@RequestMapping("/index")
	public String index2(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", "Hello Spring Boot Beetl!");
		return "index";
	}
	
	@RequestMapping("/welcome.html")
	public String welcome(Map<String, Object> model) {		
		return "welcome";
	}
	
	@GetMapping("/member-list.html")
	public String memberList(){
		return "member-list";
	}

	@PostMapping("/getUserList")
	@ResponseBody
	public LayerData<User> memberList(@RequestParam(value = "page",defaultValue = "1")Integer page,
									  @RequestParam(value = "limit",defaultValue = "10")Integer limit, ServletRequest request){
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		System.out.println(name+" "+" "+phone);
		LayerData<User> userLayerData = new LayerData<>();
		EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
		userEntityWrapper.eq("delflag","0");
		if (name != null){
			userEntityWrapper.like("name",name);
		}
		if (phone != null){
			userEntityWrapper.like("phone",phone);
		}
		Page<User> pa = new Page<>(page,limit);
		List<User> list = userService.selectList(userEntityWrapper);
		pa.setRecords(list);
		pa.setTotal(list.size());
		userLayerData.setCount(pa.getTotal());
		userLayerData.setData(pa.getRecords());
		userLayerData.setMsg("查询成功");
		return  userLayerData;
	}
	@GetMapping("/order-list.html")
	public String orderList1(){
		return "order-list";
	}

    @PostMapping("/orderList")
	@ResponseBody
    public LayerData<Order> orderList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit, ServletRequest request){
		String id = request.getParameter("id");
		//订单状态
		String  orderstate= request.getParameter("orderstate");
		//支付状态
		String	paystate= request.getParameter("paystate");
		//支付方式
		String payment= request.getParameter("payment");
		//开始时间
		String begin= request.getParameter("begin");
		//结束时间
		String over= request.getParameter("over");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(id+"---"+orderstate+"---"+paystate+"---"+payment+"---"+begin+"---"+over);
        LayerData<Order> orderLayerData = new LayerData<>();
        EntityWrapper<Order> orderEntityWrapper = new EntityWrapper<>();
        orderEntityWrapper.eq("delflag",0);
        if(id!=null&&!"".equals(id)) {
			orderEntityWrapper.like("id", id);
		}
		//订单状态
		if(orderstate!=null&&!"".equals(orderstate)) {
			orderEntityWrapper.eq("orderstate", orderstate);
		}
		//支付状态
		if(paystate!=null&&!"".equals(paystate)) {
			orderEntityWrapper.eq("paystate", paystate);
		}
		//支付方式
		if(payment!=null&&!"".equals(payment)) {
			orderEntityWrapper.eq("payment", payment);
		}
		//开始时间>=
		if(begin!=null&&!"".equals(begin)) {
			orderEntityWrapper.ge("orderTime", begin);
		}
		//结束时间<=
		if(over!=null&&!"".equals(over)) {
			orderEntityWrapper.le("orderTime", over);
		}
        Page<Order> orderPage = new Page<>(page,limit);

		List<Order> list = orderService.selectList(orderEntityWrapper);
		orderPage.setRecords(list);
		//为了防止进行条件查询时，数目不正确的问题，所以必须把条件传回去
		orderPage.setTotal(list.size());
		orderLayerData.setCount(orderPage.getTotal());
		orderLayerData.setData(orderPage.getRecords());
		orderLayerData.setMsg("查询成功");
        return  orderLayerData;
    }
	@PostMapping("/orderDel")
	@ResponseBody
	public RestResponse orderDel(@RequestParam(value = "id",required = false)int id){
		Order order = new Order();
		order.setDelflag(1);
		boolean del = orderService.update(order,new EntityWrapper<Order>().eq("id",id));
		if (del){
			return RestResponse.success();
		}
		return RestResponse.failure("该用户已经不存在了");
	}
	@PostMapping("/getCateList")
	@ResponseBody
	public LayerData<Cate> cateList(@RequestParam(value = "page",defaultValue = "1")Integer page,
									  @RequestParam(value = "limit",defaultValue = "10")Integer limit, ServletRequest request){
		LayerData<Cate> userLayerData = new LayerData<>();
		EntityWrapper<Cate> cateEntityWrapper = new EntityWrapper<>();

		Page<Cate> pageCate = new Page<>(page,limit);
		List<Cate> list = cateService.selectList(cateEntityWrapper);
		pageCate.setRecords(list);
		pageCate.setTotal(list.size());
		userLayerData.setCount(pageCate.getTotal());
		userLayerData.setData(pageCate.getRecords());
		userLayerData.setMsg("查询成功");
		return  userLayerData;
	}

	@GetMapping("/member-del.html")
	public String memberDel(){
		return "member-del";
	}

	@PostMapping("/getUserDel")
	@ResponseBody
	public LayerData<User> memberDel(@RequestParam(value = "page",defaultValue = "1")Integer page,
									 @RequestParam(value = "limit",defaultValue = "10")Integer limit, ServletRequest request){
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		System.out.println(name+" "+" "+phone);
		LayerData<User> userLayerData = new LayerData<>();
		EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
		userEntityWrapper.eq("delflag",1);
		if (name != null){
			userEntityWrapper.like("name",name);
		}
		if (phone !=null){
			userEntityWrapper.like("phone",phone);
		}
        Page<User> pa = new Page<>(page,limit);
        List<User> list = userService.selectList(userEntityWrapper);
        pa.setRecords(list);
        pa.setTotal(list.size());
        userLayerData.setCount(pa.getTotal());
        userLayerData.setData(pa.getRecords());
		userLayerData.setMsg("查询成功");
		return  userLayerData;
	}

	@PostMapping("/del")
	@ResponseBody
	public RestResponse memberDel(@RequestParam(value = "id",required = false)int id){
	    User user = new User();
	    user.setDelflag(1);
        boolean del = userService.update(user,new EntityWrapper<User>().eq("id",id));
		if (del){
			return RestResponse.success();
		}
		return RestResponse.failure("该用户已经不存在了");
	}

    @PostMapping("/stopAndStart")
    @ResponseBody
    public RestResponse memberStopAndStart(@RequestParam(value = "id",required = false)int id){
	    User u = userService.selectById(id);
        User user = new User();
	    if (u.getStatus().equals(1)){
            user.setStatus(0);
            boolean update = userService.update(user,new EntityWrapper<User>().eq("id",id));
            if (update){
                return RestResponse.success();
            }
        }
        if (u.getStatus().equals(0)){
            user.setStatus(1);
            boolean update = userService.update(user,new EntityWrapper<User>().eq("id",id));
            if (update){
                return RestResponse.success();
            }
        }


        return RestResponse.failure("该用户已经不存在了");
    }

    @PostMapping("/deleteSome")
    @ResponseBody
    public RestResponse memberDel(@RequestBody List<User> users){
        System.out.println("删除多个");
        User user = new User();
        user.setDelflag(1);
        boolean del = false;
        for (User u : users){
            del = userService.update(user,new EntityWrapper<User>().eq("id",u.getId()));
        }
        if (del){
            return RestResponse.success();
        }
        return RestResponse.failure("删除失败");
    }

	@RequestMapping("/xxx.html")
	public String xxx() {
		return "xxx";
	}
	
	@RequestMapping("/xx.html")
	public String xx() {
		return "xx";
	}
	@RequestMapping("/order-list.html")
	public String orderList(){		
		return "order-list";
	}
	
	@RequestMapping("/cate.html")
	public String cate(){		
		return "Cate";
	}
	@RequestMapping("/city.html")
	public String city() {
		return "city";
	}
	
	@RequestMapping("/admin-list.html")
	public String adminList(){
		return "admin-list";	
	}
	
	@RequestMapping("/admin-role.html")
	public String adminRole(){
		return "admin-role";	
	}
	
	@RequestMapping("/admin-cate.html")
	public String adminCate(){
		return "admin-Cate";
	}
	
	@RequestMapping("/admin-rule.html")
	public String adminRule(){
		return "admin-rule";	
	}
	
	@RequestMapping("/unicode.html")
	public String unicode(){
		return "unicode";	
	}
	
	@RequestMapping("/login.html")
	public String login(){
		return "login";	
	}
	
	@RequestMapping("/echarts1.html")
	public String echarts1(){
		return "echarts1";	
	}
	
	@RequestMapping("/echarts2.html")
	public String echarts2(){
		return "echarts2";	
	}
	
	
	
	
	@RequestMapping("/echarts3.html")
	public String echarts3(){
		return "echarts3";	
	}
	
	@RequestMapping("/echarts4.html")
	public String echarts4(){
		return "echarts4";	
	}
	@RequestMapping("/echarts5.html")
	public String echarts5(){
		return "echarts5";	
	}
	@RequestMapping("/echarts6.html")
	public String echarts6(){
		return "echarts6";	
	}
	@RequestMapping("/echarts7.html")
	public String echarts7(){
		return "echarts7";	
	}
	@RequestMapping("/echarts8.html")
	public String echarts8(){
		return "echarts8";	
	}
	
	
	@RequestMapping("/member-add.html")
	public String memberAdd(){
		return "member-add";	
	}
	
	@RequestMapping("/member-password.html")
	public String memberPassword(){
		return "member-password";	
	}
	
	@RequestMapping("/order-view.html")
	public String orderView(){
		return "order-view";	
	}
	
	
	@RequestMapping("//member-edit.html")
	public String memberEdit(){
		return "/member-edit";	
	}
	
	
	@RequestMapping("/admin-edit.html")
	public String adminEdit(){
		return "adminEdit";	
	}
	@RequestMapping("/error.html")
	public String error(){
		return "error";	
	}
	
	@RequestMapping("/role-add.html")
	public String roleAdd(){
		return "role-add";	
	}
	
	
	
	
	 
	 
		
}
